package smartcounters.androidclient;

import java.util.ArrayList;

import smartcounters.androidclient.WebServices.InformationReaderWebServiceClient;
import smartcounters.androidclient.WebServices.OnResultReceiver;
import smartcounters.androidclient.model.AccountInfo;
import smartcounters.androidclient.model.Configuration;
import smartcounters.androidclient.model.eOperationsEnum;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FindAccountActivity extends Activity implements OnClickListener, OnResultReceiver{
	public static ArrayList<AccountInfo> s_AccountsToShow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_account_layout);
        
        Button findAccountButton = (Button)findViewById(R.id.buttonFindAccount);
        findAccountButton.setOnClickListener(this);
    }

	public void onClick(View v) {
		EditText findAccountCountryEditText = (EditText)findViewById(R.id.findAccountCountryEditText);
		EditText findAccountCityEditText = (EditText)findViewById(R.id.findAccountCityEditText);
		EditText findAccountStreetEditText = (EditText)findViewById(R.id.findAccountStreetEditText);
		EditText findAccountHouseEditText = (EditText)findViewById(R.id.findAccountHouseEditText);
		Button findAccountButton = (Button)findViewById(R.id.buttonFindAccount);
		
		if (v.getId() == R.id.buttonFindAccount){
			findAccountCountryEditText.setEnabled(false);
			findAccountCityEditText.setEnabled(false);
			findAccountStreetEditText.setEnabled(false);
			findAccountHouseEditText.setEnabled(false);
			findAccountButton.setEnabled(false);
			
			TextView messageTextView = (TextView)findViewById(R.id.accountFindMessageTextView);
			messageTextView.setText("Please wait...");
			
			InformationReaderWebServiceClient readerClient = new InformationReaderWebServiceClient(Configuration.getInstance().getServerAddress());
			readerClient.RegisterResultReceiver(this);
			readerClient.GetAllAccounts(Configuration.getInstance().getUserCredentials());
		}
	}

	public void ReceiveResult(Message i_Msg) {
		EditText findAccountCountryEditText = (EditText)findViewById(R.id.findAccountCountryEditText);
		EditText findAccountCityEditText = (EditText)findViewById(R.id.findAccountCityEditText);
		EditText findAccountStreetEditText = (EditText)findViewById(R.id.findAccountStreetEditText);
		EditText findAccountHouseEditText = (EditText)findViewById(R.id.findAccountHouseEditText);
		Button findAccountButton = (Button)findViewById(R.id.buttonFindAccount);
		
		if (eOperationsEnum.values()[i_Msg.what] == eOperationsEnum.GetAllAccounts){
			ArrayList<AccountInfo> accountsToShow = new ArrayList<AccountInfo>();
			
			int house = -1;
			
			try{
				house = Integer.parseInt(findAccountHouseEditText.getText().toString());
			}
			catch (Exception ex){
				house = -1;
			}
			
			AccountInfo[] accounts = (AccountInfo[])i_Msg.obj;
			
			for (AccountInfo account : accounts){
				if (((findAccountCountryEditText.getText().toString().trim().equalsIgnoreCase("")) || (findAccountCountryEditText.getText().toString().trim().equalsIgnoreCase(account.getCountry().trim()))) &&
						((findAccountCityEditText.getText().toString().trim().equalsIgnoreCase("")) || (findAccountCityEditText.getText().toString().trim().equalsIgnoreCase(account.getCity().trim()))) &&
						((findAccountStreetEditText.getText().toString().trim().equalsIgnoreCase("")) || (findAccountStreetEditText.getText().toString().trim().equalsIgnoreCase(account.getStreet().trim()))) &&
						((house == -1) || (house == account.getHouse()))){
					accountsToShow.add(account);
				}
			}
			
			if (accountsToShow.isEmpty()){
				TextView messageTextView = (TextView)findViewById(R.id.accountFindMessageTextView);
				messageTextView.setText("No accounts found.");
			}
			else{
				TextView messageTextView = (TextView)findViewById(R.id.accountFindMessageTextView);
				messageTextView.setText("");
				
				s_AccountsToShow = accountsToShow;
				
				Intent intent = new Intent();
				intent.setClass(this, FindAccountResultsActivity.class);
				startActivity(intent);
			}
			
			findAccountCountryEditText.setEnabled(true);
			findAccountCityEditText.setEnabled(true);
			findAccountStreetEditText.setEnabled(true);
			findAccountHouseEditText.setEnabled(true);
			findAccountButton.setEnabled(true);
		}
	}
}
